package b1.mobile.blackberry.dao;

import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import b1.mobile.blackberry.controller.AgentController;
import b1.mobile.blackberry.entity.StockEntity;
import b1.mobile.blackberry.io.http.ResponseParserHelper;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.io.http.SoapContentProvider;
import b1.mobile.blackberry.ui.component.util.Capsule;

/**
 *
 * @author xiaoyu
 * <pre>
 * 	OPERA: List / Get
 * </pre>
 */

public class StockDAO extends CommonDAO{
	public static String OP_LIST = "GetItemListJSON";
	static String OP_GET = "GetItemDetail";
	public static String OP_TOTAL = "GetItemCount";
	
	public StockDAO(DataAccessDelegate delegate){
		super(delegate);
	}
	
	public void onGetResult(Serviceworker worker) {
		if(worker.responseCode == 200){
			if(worker.getOperator().equals(OP_LIST)){
				parseItemList(worker);
			} else if(worker.getOperator().equals(OP_GET)){
				parseItem(worker);
			} else if(worker.getOperator().equals(OP_TOTAL)){
				parseTotal(worker);
			}
		} else {
			
		}
	}
	
	public void list(String keyword, int pageIndex, int pageSize){
		String[] names = new String[] {"Keyword", "PageIndex", "PageSize"};
		String[] values = new String[] {keyword, String.valueOf(pageIndex), String.valueOf(pageSize)};
		SoapContentProvider soap = new SoapContentProvider(OP_LIST, names, values);
		
		try {
			Serviceworker _worker = new Serviceworker(this, AgentController.getServer(), soap);
			new Thread(_worker).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void parseItemList(Serviceworker worker){
		ResponseParserHelper help = new ResponseParserHelper(worker.responseMessage);
		JSONArray array = (JSONArray)help.parse("GetItemListJSONResult");
		
		Vector items = new Vector();
		for(int i = 0; i < array.length(); i++){
			try {
				JSONObject json = array.getJSONObject(i);
				StockEntity ety = new StockEntity(json);
				items.addElement(ety);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		_delegate.onDataReady(worker, items);
	}	
	
	public void getItem(String itemCode){
		String[] names = new String[] {"ItemCode"};
		String[] values = new String[] {itemCode};
		SoapContentProvider soap = new SoapContentProvider(OP_GET, names, values);
		
		try {
			Serviceworker worker = new Serviceworker(this, AgentController.getServer(), soap);
			new Thread(worker).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void parseItem(Serviceworker worker){
		ResponseParserHelper help = new ResponseParserHelper(worker.responseMessage);
		JSONObject json = (JSONObject)help.parse("GetItemDetailResult");
		StockEntity ety = new StockEntity(json);

		_delegate.onDataReady(worker, ety);
	}

	public void getTotal(String keyword) {
		String[] names = new String[] {"Keyword"};
		String[] values = new String[] {keyword};
		SoapContentProvider soap = new SoapContentProvider(OP_TOTAL, names, values);
		
		try {
			Serviceworker _worker = new Serviceworker(this, AgentController.getServer(), soap);
			new Thread(_worker).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void parseTotal(Serviceworker worker){
		ResponseParserHelper help = new ResponseParserHelper(worker.responseMessage);
		String countStr = (String)help.parse("GetItemCountResult");
		_delegate.onDataReady(worker, new Integer(Integer.parseInt(countStr)));
	}
}
